home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 36.2 KB | 1,437 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWPrInfo.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWPRINFO_H
- #include "FWPrInfo.h"
- #endif
-
- #ifndef FWMEMORY_H
- #include "FWMemory.h"
- #endif
-
- #ifndef FWBARRAY_H
- #include "FWBArray.h"
- #endif
-
- #ifndef FWACQUIR_H
- #include "FWAcquir.h"
- #endif
-
- #ifndef FWGXUTIL_H
- #include "FWGXUtil.h"
- #endif
-
- #ifndef FWGRUTIL_H
- #include "FWGrUtil.h"
- #endif
-
- #ifndef FWPRHDLR_H
- #include "FWPrHdlr.h"
- #endif
-
- #ifndef FWSTRS_H
- #include "FWStrs.h"
- #endif
-
- #ifndef FWSESION_H
- #include "FWSesion.h"
- #endif
-
- // ----- QuickDraw GX includes
-
- #if defined(FW_SUPPORT_GX) && !defined(__GXGRAPHICS__)
- #include <GXGraphics.h>
- #endif
-
- #if defined(FW_SUPPORT_GX) && !defined(__GXENVIRONMENT__)
- #include <GXEnvironment.h>
- #endif
-
- // ----- OpenDoc includes
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- #ifndef SOM_ODArbitrator_xh
- #include <Arbitrat.xh>
- #endif
-
- #ifndef SOM_ODFoci_xh
- #include <Foci.xh>
- #endif
-
- #ifndef SOM_ODFrame_xh
- #include <Frame.xh>
- #endif
-
- #ifndef SOM_ODWindowState_xh
- #include <WinStat.xh>
- #endif
-
- #ifndef SOM_ODStorageUnit_xh
- #include <StorageU.xh>
- #endif
-
- #ifndef SOM_ODCanvas_xh
- #include <Canvas.xh>
- #endif
-
- #ifndef SOM_ODFacet_xh
- #include <Facet.xh>
- #endif
-
- //========================================================================================
- // RunTime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfprinting
- #endif
-
- FW_DEFINE_AUTO(FW_CPrintEnvironment)
- FW_DEFINE_AUTO(FW_CPrintInfo)
- FW_DEFINE_AUTO(FW_CPrintJob)
-
- //========================================================================================
- // Local helpers
- //========================================================================================
-
- static void SaveHandle(
- Environment* ev,
- ODStorageUnit* storageUnit,
- const char* propName,
- const char* propType,
- FW_PlatformHandle handle);
-
- static void SaveBuffer(
- Environment* ev,
- ODStorageUnit* storageUnit,
- const char* propName,
- const char* propType,
- const void* buffer,
- unsigned long bufSize);
-
- static FW_Boolean LoadHandle(
- Environment* ev,
- ODStorageUnit* storageUnit,
- const char* propName,
- const char* propType,
- FW_PlatformHandle& handle);
-
- static FW_Boolean LoadBuffer(
- Environment* ev,
- ODStorageUnit* storageUnit,
- const char* propName,
- const char* propType,
- void* buffer,
- unsigned long& bufSize);
-
- #if defined(FW_SUPPORT_GX) && defined(FW_BUILD_MAC)
-
- static OSErr MacGXSpoolProc(gxShape currentShape, long refCon);
-
- struct SMacGXSpool
- {
- gxViewPort fGXViewPort;
- gxJob fGXPrintJob;
- FW_CRect fPageBounds;
- };
-
- static SMacGXSpool gMacGXSpool;
-
- #endif
-
- //========================================================================================
- // class FW_CPrintEnvironment
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintEnvironment::FW_CPrintEnvironment
- //----------------------------------------------------------------------------------------
-
- FW_CPrintEnvironment::FW_CPrintEnvironment()
- {
- #ifdef FW_SUPPORT_GX
- if (!FW_IsGXInstalled())
- {
- #endif
-
- #ifdef FW_BUILD_MAC
- ::PrOpen(); // Open the current printer driver and resource file
-
- short error = ::PrError();
- if (error == fnfErr)
- FW_Failure(FW_xNoDefaultPrinter);
- FW_FailOnError(error);
-
- fMacResFile = ::CurResFile(); // Save the driver's resource file
- #endif
-
- #ifdef FW_SUPPORT_GX
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- ::GetPort(&fOldPort);
- #endif
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintEnvironment::~FW_CPrintEnvironment
- //----------------------------------------------------------------------------------------
-
- FW_CPrintEnvironment::~FW_CPrintEnvironment()
- {
- FW_START_DESTRUCTOR
-
- #ifdef FW_BUILD_MAC
- ::SetPort(fOldPort);
- #endif
-
- #ifdef FW_SUPPORT_GX
- if (!FW_IsGXInstalled())
- {
- #endif
-
- #ifdef FW_BUILD_MAC
- ::UseResFile(fMacResFile);
- ::PrClose();
- #endif
-
- #ifdef FW_SUPPORT_GX
- }
- #endif
- }
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CPrintEnvironment::MacRestoreResFile
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintEnvironment::MacRestoreResFile()
- {
- ::UseResFile(fMacResFile);
- }
- #endif
-
- //========================================================================================
- // class FW_CPrintInfo
- //========================================================================================
-
- const char* kPrintInfoProp = "ODF:Property:PrintingInfo";
-
- #ifdef FW_BUILD_WIN
- const char* kPrintType = "Windows:Type:Print info";
- const char* kTypeDevMode = "Windows:Type:DEVMODE";
- const char* kTypeDevNames = "Windows:Type:DEVNAMES";
- #endif
-
- #ifdef FW_BUILD_MAC
- const char* kPrintType = "Macintosh:Type:TPrint Record";
- #endif
-
- #ifdef FW_SUPPORT_GX
- const char* kPrintTypeGX = "Macintosh:Type:QuickDrawGX Job";
- #endif
-
- const int kMaxPage = 9999;
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::FW_CPrintInfo
- //----------------------------------------------------------------------------------------
-
- FW_CPrintInfo::FW_CPrintInfo(
- Environment* ev,
- ODStorageUnit* storageUnit,
- FW_CPrintEnvironment* /* printEnv */)
- {
- #ifdef FW_BUILD_WIN
- WinSetPrintDlgToDefaults();
- #endif
- #ifdef FW_SUPPORT_GX
- fGXJob = NULL;
- #endif
- #ifdef FW_BUILD_MAC
- fMacPrintRec = NULL;
- #endif
-
- // ----- Check for QuickDraw GX print job
-
- #ifdef FW_SUPPORT_GX
- if (FW_IsGXInstalled())
- {
- fGXJob = LoadFromStorageUnitGX(ev, storageUnit);
- if (fGXJob != NULL)
- return;
- }
- #endif
-
- // ----- Check for platform print info structure
-
- #ifdef FW_BUILD_WIN
- unsigned long bufSize;
- if (::LoadBuffer(ev, storageUnit, kPrintInfoProp, kPrintType, &fWinPrintDlg, bufSize))
- {
- ::LoadHandle(ev, storageUnit, kPrintInfoProp, kTypeDevMode, fWinPrintDlg.hDevMode);
- ::LoadHandle(ev, storageUnit, kPrintInfoProp, kTypeDevNames, fWinPrintDlg.hDevNames);
- return;
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- THPrint thPrint = NULL;
- if (::LoadHandle(ev, storageUnit, kPrintInfoProp, kPrintType, (FW_PlatformHandle&) thPrint))
- {
- #ifdef FW_SUPPORT_GX
- if (FW_IsGXInstalled())
- {
- FW_XPrint::CheckPrintError(::GXNewJob(&fGXJob));
- ::GXConvertPrintRecord(fGXJob, thPrint);
- FW_CMemoryManager::FreeSystemHandle((FW_PlatformHandle)thPrint);
- FW_XPrint::CheckGXJobError(fGXJob);
- return;
- }
- #endif
-
- fMacPrintRec = thPrint;
-
- ::PrValidate(fMacPrintRec);
- FW_XPrint::MacCheckPrintError();
-
- return;
- }
- #endif
-
- // ----- Create default print info structure
-
- #ifdef FW_SUPPORT_GX
- if (FW_IsGXInstalled())
- {
- FW_XPrint::CheckPrintError(::GXNewJob(&fGXJob));
- return;
- }
- #endif
-
- #ifdef FW_BUILD_WIN
- WinSetPrintDlgToDefaults();
- #endif
-
- #ifdef FW_BUILD_MAC
- fMacPrintRec = (THPrint) FW_CMemoryManager::AllocateSystemHandle(sizeof(TPrint));
- ::PrintDefault(fMacPrintRec);
- #endif
-
- FW_END_CONSTRUCTOR
- }
-
- #ifdef FW_SUPPORT_GX
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::LoadFromStorageUnitGX
- //----------------------------------------------------------------------------------------
-
- gxJob FW_CPrintInfo::LoadFromStorageUnitGX(
- Environment* ev,
- ODStorageUnit* storageUnit)
- {
- gxJob job = NULL;
- FW_PlatformHandle handle = NULL;
- if (::LoadHandle(ev, storageUnit, kPrintInfoProp, kPrintTypeGX, handle))
- {
- ::GXNewJob(&job);
- ::GXUnflattenJobFromHdl(job, handle);
- FW_CMemoryManager::FreeSystemHandle(handle);
- FW_XPrint::CheckGXJobError(job);
- }
-
- return job;
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::~FW_CPrintInfo
- //----------------------------------------------------------------------------------------
-
- FW_CPrintInfo::~FW_CPrintInfo()
- {
- FW_START_DESTRUCTOR
-
- #ifdef FW_SUPPORT_GX
- if (fGXJob != NULL)
- ::GXDisposeJob(fGXJob);
- #endif
-
- #ifdef FW_BUILD_MAC
- if(fMacPrintRec != NULL)
- FW_CMemoryManager::FreeSystemHandle((FW_PlatformHandle)fMacPrintRec);
- #endif
- }
-
- #ifdef FW_BUILD_WIN
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::WinSetPrintDlgToDefaults
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintInfo::WinSetPrintDlgToDefaults()
- {
- fWinPrintDlg.lStructSize = sizeof(PRINTDLG);
- fWinPrintDlg.hDevMode = NULL;
- fWinPrintDlg.hDevNames = NULL;
- fWinPrintDlg.Flags = 0;
- fWinPrintDlg.hwndOwner = NULL;
- fWinPrintDlg.hDC = NULL;
- fWinPrintDlg.nFromPage = 1;
- fWinPrintDlg.nToPage = kMaxPage;
- fWinPrintDlg.nMinPage = 1;
- fWinPrintDlg.nMaxPage = kMaxPage;
- fWinPrintDlg.nCopies = 1;
- fWinPrintDlg.hInstance = NULL;
- fWinPrintDlg.lCustData = 0l;
- fWinPrintDlg.lpfnPrintHook = NULL;
- fWinPrintDlg.lpfnSetupHook = NULL;
- fWinPrintDlg.lpPrintTemplateName = NULL;
- fWinPrintDlg.lpSetupTemplateName = NULL;
- fWinPrintDlg.hPrintTemplate = NULL;
- fWinPrintDlg.hSetupTemplate = NULL;
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::SaveToStorageUnit
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintInfo::SaveToStorageUnit(Environment* ev, ODStorageUnit* storageUnit) const
- {
- #ifdef FW_SUPPORT_GX
- if (fGXJob != NULL)
- {
- FW_PlatformHandle handle = FW_CMemoryManager::AllocateSystemHandle(0);
- ::GXFlattenJobToHdl(fGXJob, handle);
-
- FW_TRY
- {
- ::SaveHandle(ev, storageUnit, kPrintInfoProp, kPrintTypeGX, handle);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- FW_CMemoryManager::FreeSystemHandle(handle);
-
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- FW_CMemoryManager::FreeSystemHandle(handle);
- return;
- }
- #endif
-
- #ifdef FW_BUILD_WIN
- ::SaveBuffer(ev, storageUnit, kPrintInfoProp, kPrintType, &fWinPrintDlg, sizeof fWinPrintDlg);
-
- ::SaveHandle(ev, storageUnit, kPrintInfoProp, kTypeDevNames, fWinPrintDlg.hDevNames);
- ::SaveHandle(ev, storageUnit, kPrintInfoProp, kTypeDevMode, fWinPrintDlg.hDevMode);
- #endif
- #ifdef FW_BUILD_MAC
- ::SaveHandle(ev, storageUnit, kPrintInfoProp, kPrintType, (FW_PlatformHandle)fMacPrintRec);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::GetCopyCount
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintInfo::GetCopyCount(short& copyCount, FW_Boolean& collate) const
- {
- #ifdef FW_SUPPORT_GX
- if (fGXJob != NULL)
- {
- copyCount = 1;
- collate = FALSE;
- return;
- }
- #endif
- #ifdef FW_BUILD_WIN
- copyCount = fWinPrintDlg.nCopies;
- collate = (fWinPrintDlg.Flags & PD_COLLATE) != 0;
- #endif
- #ifdef FW_BUILD_MAC
- copyCount = (*fMacPrintRec)->prJob.iCopies;
- collate = FALSE;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::GetPages
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintInfo::GetPages(long& firstPage, long& lastPage)
- {
- #ifdef FW_SUPPORT_GX
- if (fGXJob != NULL)
- {
- ::GXGetJobPageRange(fGXJob, &firstPage, &lastPage);
- return;
- }
- #endif
- #ifdef FW_BUILD_WIN
- firstPage = fWinPrintDlg.nFromPage;
- lastPage = fWinPrintDlg.nToPage;
- #endif
- #ifdef FW_BUILD_MAC
- firstPage = (*fMacPrintRec)->prJob.iFstPage;
- lastPage = (*fMacPrintRec)->prJob.iLstPage;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::GetPageBounds
- //----------------------------------------------------------------------------------------
-
- FW_CRect FW_CPrintInfo::GetPageBounds() const
- {
- #ifdef FW_SUPPORT_GX
- if (fGXJob != NULL)
- {
- gxFormat format = ::GXGetJobFormat(fGXJob, 1);
- gxRectangle pageBounds;
- ::GXGetFormatDimensions(format, &pageBounds, nil);
-
- return FW_CRect(
- FW_ODFixedToFixed(pageBounds.left),
- FW_ODFixedToFixed(pageBounds.top),
- FW_ODFixedToFixed(pageBounds.right),
- FW_ODFixedToFixed(pageBounds.bottom));
- }
- #endif
- #ifdef FW_BUILD_WIN
- // Scale the bounds
- FW_CPoint resPrint(
- FW_IntToFixed(::GetDeviceCaps(fWinPrintDlg.hDC, LOGPIXELSX)),
- FW_IntToFixed(::GetDeviceCaps(fWinPrintDlg.hDC, LOGPIXELSY)));
-
- FW_CRect pageBounds = fWinPageBounds;
-
- pageBounds.left = FW_WideMultiply(pageBounds.left, FW_kFixed72) / resPrint.x;
- pageBounds.top = FW_WideMultiply(pageBounds.top, FW_kFixed72) / resPrint.y;
- pageBounds.right = FW_WideMultiply(pageBounds.right, FW_kFixed72) / resPrint.x;
- pageBounds.bottom = FW_WideMultiply(pageBounds.bottom, FW_kFixed72) / resPrint.y;
-
- return pageBounds;
- #endif
- #ifdef FW_BUILD_MAC
- return (*fMacPrintRec)->prInfo.rPage;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::GetPaperBounds
- //----------------------------------------------------------------------------------------
-
- FW_CRect FW_CPrintInfo::GetPaperBounds() const
- {
- #ifdef FW_SUPPORT_GX
- if (fGXJob != NULL)
- {
- gxFormat format = ::GXGetJobFormat(fGXJob, 1);
- gxRectangle paperBounds;
- ::GXGetFormatDimensions(format, nil, &paperBounds);
-
- return FW_CRect(
- FW_ODFixedToFixed(paperBounds.left),
- FW_ODFixedToFixed(paperBounds.top),
- FW_ODFixedToFixed(paperBounds.right),
- FW_ODFixedToFixed(paperBounds.bottom));
- }
- #endif
- #ifdef FW_BUILD_WIN
- // Scale the bounds
- FW_CPoint resPrint(
- FW_IntToFixed(::GetDeviceCaps(fWinPrintDlg.hDC, LOGPIXELSX)),
- FW_IntToFixed(::GetDeviceCaps(fWinPrintDlg.hDC, LOGPIXELSY)));
-
- FW_CRect pageBounds = fWinPageBounds;
-
- pageBounds.left = FW_WideMultiply(pageBounds.left, FW_kFixed72) / resPrint.x;
- pageBounds.top = FW_WideMultiply(pageBounds.top, FW_kFixed72) / resPrint.y;
- pageBounds.right = FW_WideMultiply(pageBounds.right, FW_kFixed72) / resPrint.x;
- pageBounds.bottom = FW_WideMultiply(pageBounds.bottom, FW_kFixed72) / resPrint.y;
-
- return pageBounds;
- #endif
- #ifdef FW_BUILD_MAC
- return (*fMacPrintRec)->rPaper;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::GetPrinterResolution
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CPrintInfo::GetPrinterResolution(long& hRes, long& vRes) const
- {
- #ifdef FW_SUPPORT_GX
-
- if (fGXJob != NULL)
- {
- gxFormat format = ::GXGetJobFormat(fGXJob, 1);
-
- gxMapping mapping;
- ::GXGetFormatMapping(format, &mapping);
-
- // Fixed * long => long
- hRes = FixMul(mapping.map[0][0], 72L);
- vRes = FixMul(mapping.map[1][1], 72L);
-
- return ::GXGetJobError(fGXJob) == noErr;
- }
-
- #endif
- #ifdef FW_BUILD_MAC
-
- hRes = (*fMacPrintRec)->prInfo.iHRes;
- vRes = (*fMacPrintRec)->prInfo.iVRes;
- return true;
-
- #endif
- #ifdef FW_BUILD_WIN
-
- hRes = ::GetDeviceCaps(fWinPrintDlg.hDC, LOGPIXELSX);
- vRes = ::GetDeviceCaps(fWinPrintDlg.hDC, LOGPIXELSY);
- return true;
-
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::SetPrinterResolution
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_CPrintInfo::SetPrinterResolution(long hRes, long vRes)
- {
- long curHRes, curVRes;
-
- if(GetPrinterResolution(curHRes, curVRes) && hRes == curHRes && vRes == curVRes)
- return true;
-
- FW_DEBUG_MESSAGE("FW_CPrintInfo::SetPrinterResolution not implemented");
- return false;
- }
-
-
- #ifdef FW_BUILD_WIN
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::GetGXJob
- //----------------------------------------------------------------------------------------
-
- LPPRINTDLG FW_CPrintInfo::WinGetPrintDlg()
- {
- return &fWinPrintDlg;
- }
- #endif
-
- #ifdef FW_BUILD_WIN
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::GetGXJob
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintInfo::WinUpdatePageRect()
- {
- FW_ASSERT(fWinPrintDlg.hDC != NULL);
- ::GetClipBox(fWinPrintDlg.hDC, &fWinPageBounds);
- }
- #endif
-
- #ifdef FW_SUPPORT_GX
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::GetGXJob
- //----------------------------------------------------------------------------------------
-
- gxJob FW_CPrintInfo::GetGXJob() const
- {
- return fGXJob;
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_CPrintInfo::MacGetTHPrint
- //----------------------------------------------------------------------------------------
-
- THPrint FW_CPrintInfo::MacGetTHPrint() const
- {
- return fMacPrintRec;
- }
- #endif
-
- //========================================================================================
- // class FW_CPrintJob
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::FW_CPrintJob
- //----------------------------------------------------------------------------------------
-
- FW_CPrintJob::FW_CPrintJob(
- FW_CPrintInfo* printInfo,
- FW_CPrintHandler* printHandler,
- FW_CPrintEnvironment* printEnv) :
- fPrintInfo(printInfo),
- fPrintHandler(printHandler),
- fPrintEnv(printEnv)
- {
- #ifdef FW_SUPPORT_GX
- fGXViewPort = NULL;
- fGXSpoolUPP = NULL;
- fMacCanvasWindow = NULL;
- #endif
- #ifdef FW_BUILD_WIN
- fWinSaveDCIndex = 0;
- #endif
- #ifdef FW_BUILD_MAC
- fMacPrPort = NULL;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::~FW_CPrintJob
- //----------------------------------------------------------------------------------------
-
- FW_CPrintJob::~FW_CPrintJob()
- {
- #ifdef FW_SUPPORT_GX
- FW_ASSERT(fGXViewPort == NULL);
- #endif
- #ifdef FW_BUILD_MAC
- FW_ASSERT(fMacPrPort == NULL);
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::OpenDocument
- //----------------------------------------------------------------------------------------
-
- ODFacet* FW_CPrintJob::OpenDocument(Environment* ev,
- ODFrame* frame,
- long numPages,
- const FW_CString& title)
- {
- FW_ASSERT(numPages > 0);
-
- ODFacet* facet = NULL;
-
- // Create a progress dialog
- fPrintHandler->CreateProgressDialog(ev, frame, numPages, title);
-
- FW_TRY
- {
- // Create the platform print job
- PrivCreatePlatformPrintJob(numPages, title);
-
- FW_TRY
- {
- // Get the transform and shape for the page facet
- FW_CRect pageBounds = fPrintInfo->GetPageBounds();
- FW_CAcquiredODTransform aqTransform = frame->CreateTransform(ev);
- FW_CAcquiredODShape aqClipShape = frame->CreateShape(ev);
-
- aqClipShape->SetRectangle(ev, (ODRect*) &pageBounds);
-
- // Create a facet with this geometry
- facet = FW_CSession::GetWindowState(ev)->CreateFacet(ev, frame, aqClipShape,
- aqTransform, kODNULL, kODNULL);
- FW_ASSERT(facet != NULL);
-
- FW_TRY
- {
- // Create an OpenDoc canvas
- ODCanvas* canvas = PrivCreatePrintCanvas(ev, facet);
- FW_ASSERT(canvas != NULL);
-
- // Set its owner
- FW_CAcquiredODPart aqPart = frame->AcquirePart(ev);
- canvas->SetOwner(ev, aqPart);
-
- FW_TRY
- {
- // Attach the canvas to the facet
- facet->ChangeCanvas(ev, canvas);
-
- FW_TRY
- {
- // Add the facet to the frame
- frame->FacetAdded(ev, facet);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- // If the canvas has already been attached to the facet, then
- // we need to delete the facet first to prevent the facet from
- // referencing a canvas that has been deleted
- delete facet;
- facet = NULL;
- delete canvas;
- canvas = NULL;
- FW_THROW_SAME();
- }
- FW_CATCH_END
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- delete canvas;
- FW_THROW_SAME();
- }
- FW_CATCH_END
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- delete facet;
- FW_THROW_SAME();
- }
- FW_CATCH_END
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- PrivAbortPlatformPrintJob();
- FW_THROW_SAME();
- }
- FW_CATCH_END
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- fPrintHandler->DestroyProgressDialog(ev, frame);
-
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- return facet;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::PrivCreatePlatformPrintJob
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintJob::PrivCreatePlatformPrintJob(long numPages, const FW_CString& title)
- {
- #ifdef FW_SUPPORT_GX
- gxJob job = fPrintInfo->GetGXJob();
- if (job != NULL)
- {
- Str255 pascalTitle;
- title.ExportPascal(pascalTitle);
-
- ::GXStartJob(job, pascalTitle, numPages);
- FW_XPrint::CheckGXJobError(job);
-
- return;
- }
- #endif
-
- #ifdef FW_BUILD_WIN
- DOCINFO di;
-
- di.cbSize = sizeof(DOCINFO);
- di.lpszDocName = title;
- di.lpszOutput = NULL;
- di.lpszDatatype = NULL;
- di.fwType = 0;
-
- LPPRINTDLG lppd = fPrintInfo->WinGetPrintDlg();
- FW_ASSERT(lppd->hDC != NULL);
- FW_XPrint::CheckPrintError(::StartDoc(lppd->hDC, &di));
- #endif
-
- #ifdef FW_BUILD_MAC
- fPrintEnv->MacRestoreResFile();
-
- THPrint thPrint = fPrintInfo->MacGetTHPrint();
- ::PrValidate(thPrint); // Set document name
-
- {
- FW_ASSERT(fMacPrPort == NULL);
- FW_CMacTempPort tempPort;
-
- fMacPrPort = ::PrOpenDoc(thPrint, NULL, NULL);
- FW_XPrint::MacCheckPrintError();
- FW_ASSERT(fMacPrPort != NULL);
- }
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::PrivAbortPlatformPrintJob
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintJob::PrivAbortPlatformPrintJob()
- {
- #ifdef FW_SUPPORT_GX
- gxJob job = fPrintInfo->GetGXJob();
- if (job != NULL)
- {
- // Clean up QD -> GX spooling structures
- ::DisposeWindow(fMacCanvasWindow);
- fMacCanvasWindow = NULL;
-
- DisposeRoutineDescriptor(fGXSpoolUPP);
-
- ::GXSetJobError(job, gxPrUserAbortErr);
- ::GXFinishJob(job);
- ::GXDisposeViewPort(fGXViewPort);
- fGXViewPort = NULL;
-
- return;
- }
- #endif
- #ifdef FW_BUILD_WIN
- LPPRINTDLG lppd = fPrintInfo->WinGetPrintDlg();
- FW_ASSERT(lppd->hDC != NULL);
-
- // Abort the print job
- int error = ::AbortDoc(lppd->hDC);
- ::DeleteDC(lppd->hDC);
- lppd->hDC = NULL;
- #endif
- #ifdef FW_BUILD_MAC
- FW_ASSERT(fMacPrPort != NULL);
- FW_CMacTempPort tempPort((GrafPtr)fMacPrPort);
-
- fPrintEnv->MacRestoreResFile();
-
- ::PrSetError(iPrAbort);
- ::PrCloseDoc(fMacPrPort);
- fMacPrPort = NULL;
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::PrivCreatePrintCanvas
- //----------------------------------------------------------------------------------------
-
- ODCanvas* FW_CPrintJob::PrivCreatePrintCanvas(Environment* ev, ODFacet* facet)
- {
- ODCanvas* canvas;
-
- #ifdef FW_SUPPORT_GX
- gxJob job = fPrintInfo->GetGXJob();
- if (job != NULL)
- {
- fGXViewPort = ::GXNewViewPort(gxScreenViewDevices);
-
- // DTC -- Coercing fGXViewPort distresses the compier since gxViewPort is a pointer
- // to a partially defined struct. This is OK, the Apple headers partially define
- // it in order to prevent direct access.
- #if (defined __SC__ || defined __MRC__) && !defined _WINDOWS
- #pragma options(!warn_cast_incomplete_type) // pragma will be in effect for remainder of function
- #endif
- canvas = facet->CreateCanvas(ev, kODQuickDrawGX,
- (ODPlatformCanvas) fGXViewPort, kODFalse, kODFalse);
- FW_ASSERT(canvas != NULL);
-
- gxJob job = fPrintInfo->GetGXJob();
- FW_ASSERT(job != NULL);
- canvas->SetPlatformPrintJob(ev, kODQuickDrawGX, job);
-
- // Create a new QuickDraw window to draw to
- FW_CPlatformRect boundsWindow = fPrintInfo->GetPageBounds();
- fMacCanvasWindow = ::NewCWindow(NULL, &boundsWindow, "\p", FALSE,
- documentProc, NULL, FALSE, 0l);
-
- // Create a UPP for the spool proc
- fGXSpoolUPP = NewgxShapeSpoolProc(MacGXSpoolProc);
-
- // Set the QuickDraw canvas in the OD Canvas
- canvas->SetPlatformCanvas(ev, kODQuickDraw, fMacCanvasWindow);
- return canvas;
- }
- #endif
-
- #ifdef FW_BUILD_WIN
- LPPRINTDLG lppd = fPrintInfo->WinGetPrintDlg();
- canvas = facet->CreateCanvas(ev, kODWindows,
- lppd->hDC, kODFalse, kODFalse);
- canvas->SetPlatformPrintJob(ev, kODWindows, lppd);
- #endif
- #ifdef FW_BUILD_MAC
- canvas = facet->CreateCanvas(ev, kODQuickDraw,
- (ODPlatformCanvas) fMacPrPort, kODFalse, kODFalse);
-
- THPrint thPrint = fPrintInfo->MacGetTHPrint();
- FW_ASSERT(thPrint != NULL);
-
- canvas->SetPlatformPrintJob(ev, kODQuickDraw, thPrint);
- #endif
-
- return canvas;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::CloseDocument
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintJob::CloseDocument(
- Environment* ev,
- ODFacet* facet)
- {
- DoCloseDocument(ev, facet, TRUE);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::CloseDocumentOnError
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintJob::CloseDocumentOnError(
- Environment* ev,
- ODFacet* facet)
- {
- DoCloseDocument(ev, facet, FALSE);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::OpenPage
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintJob::OpenPage(long pageNumber)
- {
- FW_ASSERT(pageNumber > 0);
-
- #ifdef FW_SUPPORT_GX
- gxJob job = fPrintInfo->GetGXJob();
- if (job != NULL)
- {
- // Start a new page
- ::GXStartPage(job, pageNumber, ::GXGetJobFormat(job, 1), 1, &fGXViewPort);
- FW_XPrint::CheckGXJobError(job);
-
- // Install QuickDraw -> QuickDraw GX translator
- gMacGXSpool.fGXViewPort = fGXViewPort;
- gMacGXSpool.fGXPrintJob = job;
- gMacGXSpool.fPageBounds = fPrintInfo->GetPageBounds();
-
- FW_CPlatformRect pageRect = gMacGXSpool.fPageBounds;
- FW_CPlatformPoint styleStretch(1, 1);
-
- FW_ASSERT(fMacCanvasWindow != NULL);
- FW_ASSERT(fGXSpoolUPP != NULL);
-
- ::GXInstallQDTranslator(fMacCanvasWindow,
- gxDefaultOptionsTranslation,
- &pageRect, &pageRect,
- styleStretch,
- fGXSpoolUPP,
- NULL);
-
- return;
- }
- #endif
- #ifdef FW_BUILD_WIN
- LPPRINTDLG lppd = fPrintInfo->WinGetPrintDlg();
- FW_ASSERT(lppd->hDC != NULL);
- FW_XPrint::CheckPrintError(::StartPage(lppd->hDC));
-
- // Restore the DC
- if (fWinSaveDCIndex != 0)
- {
- ::RestoreDC(lppd->hDC, fWinSaveDCIndex);
- fWinSaveDCIndex = 0;
- }
- #endif
- #ifdef FW_BUILD_MAC
- {
- FW_ASSERT(fMacPrPort != NULL);
- FW_CMacTempPort tempPort((GrafPtr)fMacPrPort);
-
- fPrintEnv->MacRestoreResFile();
-
- ::PrOpenPage(fMacPrPort, NULL);
-
- FW_XPrint::MacCheckPrintError();
- }
- #endif
-
- // Show current page number
- fPrintHandler->UpdateProgressDialog(pageNumber);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::ClosePage
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintJob::ClosePage()
- {
- DoClosePage(TRUE);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::ClosePageOnError
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintJob::ClosePageOnError()
- {
- DoClosePage(FALSE);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::FinishPrinting
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintJob::FinishPrinting()
- {
- #ifdef FW_SUPPORT_GX
- gxJob job = fPrintInfo->GetGXJob();
- if (job != NULL)
- return;
- #endif
-
- #ifdef FW_BUILD_MAC
- FW_ASSERT(fMacPrPort == NULL);
-
- THPrint thPrint = fPrintInfo->MacGetTHPrint();
- if ((*thPrint)->prJob.bJDocLoop == bSpoolLoop)
- {
- TPrStatus printStatus;
- ::PrPicFile(thPrint, NULL, NULL, NULL, &printStatus);
- }
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::DoCloseDocument
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintJob::DoCloseDocument(
- Environment* ev,
- ODFacet* facet,
- FW_Boolean checkErrors)
- {
- ODCanvas* canvas = facet->GetCanvas(ev);
- ODFrame* frame = facet->GetFrame(ev);
-
- frame->FacetRemoved(ev, facet);
- delete canvas;
- delete facet;
-
- FW_TRY
- {
- #ifdef FW_SUPPORT_GX
- gxJob job = fPrintInfo->GetGXJob();
- if (job != NULL)
- {
- // Finish the job
- ::GXFinishJob(job);
-
- // Clean up QD -> GX spooling structures
- ::DisposeWindow(fMacCanvasWindow);
- fMacCanvasWindow = NULL;
- DisposeRoutineDescriptor(fGXSpoolUPP);
-
- // Dispose the viewport
- ::GXDisposeViewPort(fGXViewPort);
- fGXViewPort = NULL;
-
- if (checkErrors)
- FW_XPrint::CheckGXJobError(job);
- }
- else
- #endif
- {
- #ifdef FW_BUILD_WIN
- LPPRINTDLG lppd = fPrintInfo->WinGetPrintDlg();
- FW_ASSERT(lppd->hDC != NULL);
-
- // Finish the print job
- int error = ::EndDoc(lppd->hDC);
- ::DeleteDC(lppd->hDC);
- lppd->hDC = NULL;
- if (checkErrors)
- FW_XPrint::CheckPrintError(error);
- #endif
- #ifdef FW_BUILD_MAC
- fPrintEnv->MacRestoreResFile();
-
- {
- FW_ASSERT(fMacPrPort != NULL);
- FW_CMacTempPort tempPort((GrafPtr)fMacPrPort);
-
- ::PrCloseDoc(fMacPrPort);
- fMacPrPort = NULL;
- }
-
- if (checkErrors)
- FW_XPrint::MacCheckPrintError();
- #endif
- }
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- // Close the progress dialog
- fPrintHandler->DestroyProgressDialog(ev, frame);
- FW_THROW_SAME();
- }
- FW_CATCH_END
-
- // Close the progress dialog
- fPrintHandler->DestroyProgressDialog(ev, frame);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrintJob::DoClosePage
- //----------------------------------------------------------------------------------------
-
- void FW_CPrintJob::DoClosePage(
- FW_Boolean checkErrors)
- {
- #ifdef FW_SUPPORT_GX
- gxJob job = fPrintInfo->GetGXJob();
- if (job != NULL)
- {
- // Remove the QD->GX translator; this flushes the GX shape queue
- gxTranslationStatistic statistic;
- ::GXRemoveQDTranslator(fMacCanvasWindow, &statistic);
-
- // Finish the page
- ::GXFinishPage(job);
-
- if (checkErrors)
- FW_XPrint::CheckGXJobError(job);
-
- return;
- }
- #endif
- #ifdef FW_BUILD_WIN
- LPPRINTDLG lppd = fPrintInfo->WinGetPrintDlg();
- FW_ASSERT(lppd->hDC != NULL);
-
- // Save the DC so it can be restored at the start of next page
- fWinSaveDCIndex = ::SaveDC(lppd->hDC);
-
- int error = ::EndPage(lppd->hDC);
- if (checkErrors)
- FW_XPrint::CheckPrintError(error);
- #endif
- #ifdef FW_BUILD_MAC
- {
- FW_ASSERT(fMacPrPort != NULL);
- FW_CMacTempPort tempPort((GrafPtr)fMacPrPort);
-
- fPrintEnv->MacRestoreResFile();
-
- ::PrClosePage(fMacPrPort);
- }
-
- if (checkErrors)
- FW_XPrint::MacCheckPrintError();
- #endif
- }
-
- //----------------------------------------------------------------------------------------
- // FW_XPrint::CheckPrintError
- //----------------------------------------------------------------------------------------
-
- void FW_XPrint::CheckPrintError(FW_PlatformError error)
- {
- #ifdef FW_SUPPORT_GX
- if (error == gxPrUserAbortErr)
- FW_Failure(FW_xPrintingCanceled);
- #endif
- #ifdef FW_BUILD_WIN
- if (error == SP_USERABORT)
- FW_Failure(FW_xPrintingCanceled);
-
- if (error < 0)
- FW_Failure(error);
- #endif
- #ifdef FW_BUILD_MAC
- if (error == iPrAbort)
- FW_Failure(FW_xPrintingCanceled);
-
- if(error != 0)
- FW_Failure(error);
- #endif
- }
-
- #ifdef FW_SUPPORT_GX
- //----------------------------------------------------------------------------------------
- // FW_XPrint::CheckGXJobError
- //----------------------------------------------------------------------------------------
-
- void FW_XPrint::CheckGXJobError(gxJob job)
- {
- CheckPrintError(::GXGetJobError(job));
- }
- #endif
-
- #ifdef FW_BUILD_MAC
- //----------------------------------------------------------------------------------------
- // FW_XPrint::MacCheckPrintError
- //----------------------------------------------------------------------------------------
-
- void FW_XPrint::MacCheckPrintError()
- {
- short error = ::PrError();
- CheckPrintError(error);
- }
- #endif
-
- #if defined(FW_SUPPORT_GX) && defined(FW_BUILD_MAC)
-
- //----------------------------------------------------------------------------------------
- // MacGXSpoolProc
- //----------------------------------------------------------------------------------------
-
- OSErr MacGXSpoolProc(gxShape shape, long /* refCon */)
- {
- gxShapeType shapeType = ::GXGetShapeType(shape);
-
- if (shapeType == gxEmptyType ||
- shapeType == gxFullType ||
- shapeType == gxPictureType ||
- ::GXTouchesBoundsShape((gxRectangle*)&gMacGXSpool.fPageBounds, shape))
- {
- ::GXSetShapeViewPorts(shape, 1, &gMacGXSpool.fGXViewPort);
- ::GXDrawShape(shape);
- ::GXSetShapeViewPorts(shape, 0, NULL);
- }
-
- return (OSErr) ::GXGetGraphicsError(NULL);
- }
- #endif
-
- //----------------------------------------------------------------------------------------
- // SaveHandle
- //----------------------------------------------------------------------------------------
-
- void SaveHandle(
- Environment* ev,
- ODStorageUnit* storageUnit,
- const char* propName,
- const char* propType,
- FW_PlatformHandle handle)
- {
- if (handle == NULL)
- {
- if (storageUnit->Exists(ev, (ODType) propName, (ODType) propType, 0))
- {
- storageUnit->Focus(ev, (ODType) propName, kODPosUndefined, (ODType) propType, 0, kODPosUndefined);
- storageUnit->Remove(ev);
- }
- }
- else
- {
- FW_CAcquireLockedSystemHandle lock(handle);
-
- void* buffer = lock.GetPointer();
- unsigned long bufSize = FW_CMemoryManager::GetSystemHandleSize(handle);
-
- ::SaveBuffer(ev, storageUnit, propName, propType, buffer, bufSize);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // SaveBuffer
- //----------------------------------------------------------------------------------------
-
- void SaveBuffer(
- Environment* ev,
- ODStorageUnit* storageUnit,
- const char* propName,
- const char* propType,
- const void* buffer,
- unsigned long bufSize)
- {
- if (storageUnit->Exists(ev, (ODType) propName, (ODType) propType, 0))
- storageUnit->Focus(ev, (ODType) propName, kODPosUndefined, (ODType) propType, 0, kODPosUndefined);
- else
- storageUnit->AddProperty(ev, (ODType) propName)->AddValue(ev, (ODType) propType);
-
- unsigned long oldSize = storageUnit->GetSize(ev);
-
- storageUnit->SetOffset(ev, 0);
-
- FW_CByteArray byteArray(buffer, bufSize);
- storageUnit->SetValue(ev, byteArray);
-
- if (oldSize > bufSize)
- storageUnit->DeleteValue(ev, oldSize - bufSize);
- }
-
- //----------------------------------------------------------------------------------------
- // LoadHandle
- //----------------------------------------------------------------------------------------
-
- FW_Boolean LoadHandle(
- Environment* ev,
- ODStorageUnit* storageUnit,
- const char* propName,
- const char* propType,
- FW_PlatformHandle& handle)
- {
- if (storageUnit->Exists(ev, (ODType) propName, (ODType) propType, 0))
- {
- storageUnit->Focus(ev, (ODType) propName, kODPosUndefined, (ODType) propType, 0, kODPosUndefined);
- unsigned long size = storageUnit->GetSize(ev);
- FW_CAcquireTemporarySystemHandle tempHandle(size);
- void* buffer = tempHandle.GetPointer();
-
- unsigned long sizeLoaded = 0;
- if (::LoadBuffer(ev, storageUnit, propName, propType, buffer, sizeLoaded))
- {
- handle = tempHandle.GetPlatformHandle();
- tempHandle.Orphan();
- return TRUE;
- }
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // LoadBuffer
- //----------------------------------------------------------------------------------------
-
- FW_Boolean LoadBuffer(
- Environment* ev,
- ODStorageUnit* storageUnit,
- const char* propName,
- const char* propType,
- void* buffer,
- unsigned long& bufSize)
- {
- if (storageUnit->Exists(ev, (ODType) propName, (ODType) propType, 0))
- {
- storageUnit->Focus(ev, (ODType) propName, kODPosUndefined, (ODType) propType, 0, kODPosUndefined);
- storageUnit->SetOffset(ev, 0);
-
- bufSize = storageUnit->GetSize(ev);
-
- #if FW_OPENDOC_VERSION >= FW_OPENDOC_DR3
- FW_CByteArray byteArray;
- storageUnit->GetValue(ev, bufSize, byteArray);
- byteArray.CopyBuffer(buffer, bufSize);
- #else
- FW_CByteArray byteArray(buffer, bufSize);
- storageUnit->GetValue(ev, byteArray);
- #endif
-
- return TRUE;
- }
-
- return FALSE;
- }
-
-